package com.aimao.blog.controller;

import com.aimao.blog.util.SitemapFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author PuaChen
 * @Create 2018-08-01 17:00
 */
@Controller
public class OtherController {

    @Autowired
    private SitemapFactory sitemapFactory;

    ///////////////////////////////////////////////////////////////////////////
    // SEO优化
    ///////////////////////////////////////////////////////////////////////////

    @RequestMapping("/sitemap.xml")
    public void sitemapForXML(HttpServletResponse response) throws IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        response.setHeader("Content-type", "text/xml;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        outputStream.write(sitemapFactory.getResultForXML().getBytes("UtF-8"));
        outputStream.flush();
        outputStream.close();
    }

    @RequestMapping("/sitemap.html")
    public void sitemapForHtml(HttpServletResponse response) throws IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        outputStream.write(sitemapFactory.getResultForHtml().getBytes("UTF-8"));
        outputStream.flush();
        outputStream.close();
    }

}
